/**
 * 2015年9月7日上午10:13:13
 * GarbageCalculator.javaGarbageCalculator
 */
package com.service.calculate.vehicle;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.service.bean.device.Vehicle;
import com.service.bean.garbage.GarbageBean;
import com.service.socket.protocol.Packet;

/**
 * 2015年9月7日上午10:13:13
 *
 * 张
 */
public class GarbageCalculator {
	
	private String data;
	private GarbageBean bean1;
	private GarbageBean bean2;
	public GarbageCalculator(Packet packet,Vehicle vehicle) {
		if(vehicle!=null){
			this.data = packet.getRawData();
			bean1 = new GarbageBean();
			bean2 = new GarbageBean();
			bean1.setRawData(data);
			bean2.setRawData(data);
			bean1.setGpsNo(Long.parseLong(packet.getDeviceNo()));
			bean2.setGpsNo(Long.parseLong(packet.getDeviceNo()));
			bean1.setEquipmentId(vehicle.equipmentId);
			bean2.setEquipmentId(vehicle.equipmentId);
		}
	}
	
	/**
	 * 原始数据Demo :554000FF0102000111503000180000000000000000000000000000000000000000000000000000000000000F061D082534EFD20000E0B14B0360CCFA0C38C555
	 * 55  43 00 FF 01 02 
	 * 终端编号:00013093775171 
	 * 驾驶员编号:303030303530323230363030303031 
	 * 垃圾桶编号:33313033370000000000000A000000 
	 * 日期:0F06030E1624 
	 * 垃圾毛重00003839 
	 * 维度:3F038010 
	 * 经度:E30C83AD  
	 * 0f
	 * 0c
	 * 55
	 * @return
	 */
	public List<GarbageBean> calculator(){
		List<GarbageBean> gbList = new ArrayList<GarbageBean>();
		//驾驶员编号
		this.calculateDriverNumber();
		//获取gps时间
		this.calculateGpsTime();
		//坐标(经度、维度)
		this.calculatePoint();
		//获取垃圾桶1编号
		this.calculateGarbageNumber1();
		//垃圾桶1重量
		this.calculateWeight1();
		//获取垃圾桶2编号
		this.calculateGarbageNumber2();
		//垃圾桶2重量
		this.calculateWeight2();
		gbList.add(bean1);
		gbList.add(bean2);
		return gbList;
	}
	
	/**
	 * 驾驶员编号
	 */
	private void calculateDriverNumber() {
		String no1 = (char) Integer.parseInt(data.substring(26, 28), 16)+"";
		String no2 = (char) Integer.parseInt(data.substring(28, 30), 16)+"";
		String no3 = (char) Integer.parseInt(data.substring(30, 32), 16)+"";
		String no4 = (char) Integer.parseInt(data.substring(32, 34), 16)+"";
		String no5 = (char) Integer.parseInt(data.substring(34, 36), 16)+"";
		String no6 = (char) Integer.parseInt(data.substring(36, 38), 16)+"";
		String no7 = (char) Integer.parseInt(data.substring(38, 40), 16)+"";
		String no8 = (char) Integer.parseInt(data.substring(40, 42), 16)+"";
		String no9 = (char) Integer.parseInt(data.substring(42, 44), 16)+"";
		String no10 = (char) Integer.parseInt(data.substring(44, 46), 16)+"";
		String no11 = (char) Integer.parseInt(data.substring(46, 48), 16)+"";
		String no12 = (char) Integer.parseInt(data.substring(48, 50), 16)+"";
		String no13 = (char) Integer.parseInt(data.substring(50, 52), 16)+"";
		String no14 = (char) Integer.parseInt(data.substring(52, 54), 16)+"";
		String no15 = (char) Integer.parseInt(data.substring(54, 56), 16)+"";
		String driverNumber = no1+no2+no3+no4+no5+no6+no7+no8+no9+no10+no11+no12+no13+no14+no15;
		bean1.setDriverNumber(driverNumber.toString());
		bean2.setDriverNumber(driverNumber.toString());
	}

	/**
	 * 垃圾桶1编号
	 */
	private void calculateGarbageNumber1() {
		String no1 = (char) Integer.parseInt(data.substring(56, 58), 16)+"";
		String no2 = (char) Integer.parseInt(data.substring(58, 60), 16)+"";
		String no3 = (char) Integer.parseInt(data.substring(60, 62), 16)+"";
		String no4 = (char) Integer.parseInt(data.substring(62, 64), 16)+"";
		String no5 = (char) Integer.parseInt(data.substring(64, 66), 16)+"";
		String no6 = (char) Integer.parseInt(data.substring(66, 68), 16)+"";
		String no7 = (char) Integer.parseInt(data.substring(68, 70), 16)+"";
		String no8 = (char) Integer.parseInt(data.substring(70, 72), 16)+"";
		String no9 = (char) Integer.parseInt(data.substring(72, 74), 16)+"";
		String no10 = (char) Integer.parseInt(data.substring(74, 76), 16)+"";
		String no11 = (char) Integer.parseInt(data.substring(76, 78), 16)+"";
		String no12 = (char) Integer.parseInt(data.substring(78, 80), 16)+"";
		String no13 = (char) Integer.parseInt(data.substring(80, 82), 16)+"";
		String no14 = (char) Integer.parseInt(data.substring(82, 84), 16)+"";
		String no15 = (char) Integer.parseInt(data.substring(84, 86), 16)+"";
		String rfid = no1+no2+no3+no4+no5+no6+no7+no8+no9+no10+no11+no12+no13+no14+no15;
		bean1.setRfid(rfid);
	}
	/***
	 * 垃圾桶1重量
	 */
	private void  calculateWeight1() {
		String s1 = data.substring(92,94)+data.substring(90,92)+data.substring(88,90)+data.substring(86,88);
		double collectionAmt1 = new BigDecimal(Integer.parseInt(s1, 16)/100.00).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		bean1.setCollectionAmt(collectionAmt1);
	}
	
	/**
	 * 垃圾桶2编号
	 */
	private void calculateGarbageNumber2() {
		String no1 = (char) Integer.parseInt(data.substring(94, 96), 16)+"";
		String no2 = (char) Integer.parseInt(data.substring(96, 98), 16)+"";
		String no3 = (char) Integer.parseInt(data.substring(98, 100), 16)+"";
		String no4 = (char) Integer.parseInt(data.substring(100, 102), 16)+"";
		String no5 = (char) Integer.parseInt(data.substring(102, 104), 16)+"";
		String no6 = (char) Integer.parseInt(data.substring(104, 106), 16)+"";
		String no7 = (char) Integer.parseInt(data.substring(106, 108), 16)+"";
		String no8 = (char) Integer.parseInt(data.substring(108, 110), 16)+"";
		String no9 = (char) Integer.parseInt(data.substring(110, 112), 16)+"";
		String no10 = (char) Integer.parseInt(data.substring(112,114), 16)+"";
		String no11 = (char) Integer.parseInt(data.substring(114, 116), 16)+"";
		String no12 = (char) Integer.parseInt(data.substring(116, 118), 16)+"";
		String no13 = (char) Integer.parseInt(data.substring(118, 120), 16)+"";
		String no14 = (char) Integer.parseInt(data.substring(120, 122), 16)+"";
		String no15 = (char) Integer.parseInt(data.substring(122, 124), 16)+"";
		String rfid = no1+no2+no3+no4+no5+no6+no7+no8+no9+no10+no11+no12+no13+no14+no15;
		bean2.setRfid(rfid);
	}
	/***
	 * 垃圾桶2重量
	 */
	private void  calculateWeight2() {
		String s2 = data.substring(130,132)+data.substring(128,130)+data.substring(126,128)+data.substring(124,126);
		double collectionAmt2 = new BigDecimal(Integer.parseInt(s2, 16)/100.00).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		bean2.setCollectionAmt(collectionAmt2);
	}
	
	/****
	 * 日期
	 */
	private void calculateGpsTime() {
		StringBuilder builder = new StringBuilder();
		builder.append(2000+Integer.parseInt(data.substring(132, 134), 16));
		builder.append("-");
		builder.append(Integer.parseInt(data.substring(134, 136), 16));
		builder.append("-");
		builder.append(Integer.parseInt(data.substring(136, 138), 16));
		builder.append(" ");
		builder.append(Integer.parseInt(data.substring(138, 140), 16));
		builder.append(":");
		builder.append(Integer.parseInt(data.substring(140, 142), 16));
		builder.append(":");
		builder.append(Integer.parseInt(data.substring(142, 144), 16));
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = dateFormat.parse(builder.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date now =  new Date();
		if(date.getTime() - now.getTime() > 5*60*1000){
			bean1.setGpsTime(now);
			bean2.setGpsTime(now);
		}else{
			bean1.setGpsTime(date);
			bean2.setGpsTime(date);
		}
	}
	
	/**
	 * gps坐标计算
	 * @param data
	 * @param bean
	 */
	private void calculatePoint(){
		String latitude = data.substring(150,152)+data.substring(148,150)+data.substring(146,148)+data.substring(144,146);
		String longitude = data.substring(158,160)+data.substring(156,158)+data.substring(154,156)+data.substring(152,154);			
		bean1.setLat(new BigDecimal(Integer.parseInt(latitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue());
		bean1.setLng(new BigDecimal(Integer.parseInt(longitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue());
		bean2.setLat(new BigDecimal(Integer.parseInt(latitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue());
		bean2.setLng(new BigDecimal(Integer.parseInt(longitude, 16) / 1800000.00).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue());
	}
}




